####
#
# Copyright 2007 B. Chandler Carruth
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
####
#
# ./lib/rcons/build.rb
#
# This file provides a build object. This organizes the build, and
# provides methods for initiating a build of a particular target. It
# also ensures all configuration and/or setup is taken care of for the
# build.
#
####

# Files that must be imported in order for the build file interpreter to work
require( 'rcons/target' )
require( 'rcons/environment' )
require( 'rcons/targets/file_target' )
require( 'rcons/targets/source_file_target' )
require( 'rcons/tool_target' )
require( 'rcons/tools/compiler_target' )
require( 'rcons/tools/c_cxx/c_compiler_target' )
require( 'rcons/tools/c_cxx/c_source_file_target' )
require( 'rcons/tools/c_cxx/c_application_target' )
require( 'rcons/tools/c_cxx/cxx_compiler_target' )
require( 'rcons/tools/c_cxx/cxx_source_file_target' )
require( 'rcons/tools/c_cxx/cxx_application_target' )
require( 'rcons/tools/gcc/gcc_c_compiler_target' )
require( 'rcons/tools/gcc/gcc_cxx_compiler_target' )


module RCons

  # Organizes the build process and provides methods for initiating the
  # build of particular targets associated with a build.  Configuration
  # for the build is also handled.
  #
  # The build is executed within this context, allowing all ruby and
  # RCons methods to be available.  Thus, they can be used in the
  # buildfile itself.
  #
  # Extends the +Target+ class.
  class Build < Target
    attr_reader( :targets )

    # Initializes the Build object from a specified build file.  Much of the
    # initialization work is done by the build file itself.
    #
    # Inputs:
    # +build_file+:: a ruby script specifying the build parameters and process.
    def initialize( build_file )
      # Prepare vars, etc. for interpreting
      build_env = Environment.new( 'default', File::dirname( build_file ), '.' )

      # Setup default build target variables
      name = 'default'
      opts = {}
      opts[ :brief ] = 'Default build target'
      opts[ :long ] = <<EOT
This is the default build target. All targets specifically added to the default
target in the buildfile will be built when no targets are specified.
EOT

      # call up to Target
      super( build_env, name, opts )

      # Attempt to open project build file
      build_script = nil
      File.open( build_file ) do |f|
        build_script = f.read()
      end

      # If build file was successfully opened, evaluate and parse
      if build_script != nil
        eval( build_script )
      end

    end

    # An alias for the +add_pre_dep+ method.
    def add( t )
      add_pre_dep( t )
    end

    # An alias for the +print+ method.
    def p( *args )
      @build_env.print( *args )
    end

    # An alias for the +printv+ method.
    def pv( v, *args )
      @build_env.printv( v, *args )
    end
  end
end

